package cn.keepme.common.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.keepme.common.consts.PublicOPCode;
import cn.keepme.common.consts.PublicParams;

public class MyServletContextListener implements ServletContextListener {

	private static Log log = LogFactory.getLog(MyServletContextListener.class);
	private ServletContext context;
	
	// 实现其中的销毁函数  
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {

	}

	// 实现其中的初始化函数，当有事件发生时即触发  
	@Override
	public void contextInitialized(ServletContextEvent servletContextEvent) {
		context = servletContextEvent.getServletContext();
		/*在服务启动时，将数据库中的数据加载进内存，并将其赋值给一个属性名，其它的 Servlet 就可以通过 getAttribute 进行属性值的访问*/
//		PublicParams.CONTEXT_PATH = context.getContextPath();
//		PublicParams.REAL_PATH = context.getRealPath("");
		
		// 加载允许访问的网关IP列表
		PublicParams.ALLOWIPS = PublicOPCode.readAllowips(context.getRealPath("/") 
				+ "WEB-INF/classes/allowips.txt");
		log.info("initialized.加载允许访问的网关IP列表完毕！");
		
		System.out.println("====== listener initialized end =========");
	}
	
	/*
	private void getCount(){
		try{  
	       // 从文件中读取计数器的数值  
	       BufferedReader reader=new BufferedReader(  
	           new InputStreamReader(context.  
	           getResourceAsStream("/count/count.txt")));  
	       int count=Integer.parseInt(reader.readLine());  
	       reader.close();  
	       // 创建计数器对象  
	       Counter counter=new Counter(count);  
	       // 把计数器对象保存到 Web 应用范围  
	       context.setAttribute("counter",counter);  
	    } catch(IOException e) {  
	          e.printStackTrace();  
	    }
	}
	*/

}
